import rarfile
import itertools
import string
import time

# 密码字符集
characters = string.ascii_letters + string.digits + string.punctuation

# 密码长度范围
min_length = 1
max_length = 20

def brute_force_rar_password(rar_path, charset, min_len, max_len):
    start_time = time.time()
    with rarfile.RarFile(rar_path) as rf:
        for length in range(min_len, max_len + 1):
            for pwd in itertools.product(charset, repeat=length):
                password = ''.join(pwd)
                try:
                    rf.setpassword(password)
                    if rf.needs_password:
                        print(f"Trying password: {password}...")
                    if rf.needs_password is False:
                        print(f"Password found: {password}")
                        return password
                    else:
                        rf.setpassword(None)
                except rarfile.BadRarFile:
                    print(f"Password not valid: {password}")
                except KeyboardInterrupt:
                    print("Brute-force interrupted by user.")
                    return None
    print("Password not found.")
    return None

# 要破解的RAR文件路径
rar_path = r'I:\003_Material\Data\XR_No.680.rar'

# 开始暴力破解
password = brute_force_rar_password(rar_path, characters, min_length, max_length)
print(password)